//
//  FZPasswordViewController.m
//  oo
//
//  Created by apple on 2017/10/12.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "FZPasswordViewController.h"
#import "FZNewPaswViewController.h"


@interface FZPasswordViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UIButton *captchaBtn;
@property (weak, nonatomic) IBOutlet UITextField *captchaTextField;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
@property (weak, nonatomic) IBOutlet UILabel *sendLab;
@property (weak, nonatomic) IBOutlet UILabel *timeLab;
@property (weak, nonatomic) IBOutlet UIView *phoneView;

@end

@implementation FZPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.


    _phoneTextField.delegate = self;
    [_phoneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    _captchaTextField.delegate = self;
    [_captchaTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
- (IBAction)captchaSelected:(UIButton *)sender {
//    if (_phoneTextField.text.length != 11) {
//
//    }else{
        [self getVerificationCode];
//    }
}

- (IBAction)nextSelected:(UIButton *)sender {
    FZNewPaswViewController *newPaswVC = [FZNewPaswViewController new];
    [self.navigationController pushViewController:newPaswVC animated:YES];
}
- (void)textFieldDidChange:(UITextField *)textField{
    if (textField == _phoneTextField) {
     
    }else{
        if (_captchaTextField.text.length != 6) {
            
        }else{
            _nextBtn.backgroundColor =[UIColor orangeColor];
        }
    }
}
-(void)getVerificationCode{

    _captchaBtn.enabled = NO;
    _phoneView.hidden = YES;
    _sendLab.hidden = NO;
    [_captchaBtn setTitle:@"倒计时         "forState:UIControlStateNormal];
    _timeLab.hidden = NO;
    
    _sendLab.text = [NSString stringWithFormat:@"验证码已发送到%@的手机上",_phoneTextField.text];
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:_sendLab.text];
    NSUInteger firstLoc = [[noteStr string] rangeOfString:@"到"].location + 1;
    NSUInteger secondLoc = [[noteStr string] rangeOfString:@"的"].location;
    NSRange range = NSMakeRange(firstLoc, secondLoc - firstLoc);
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:range];
    [noteStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:range];
    [_sendLab setAttributedText:noteStr];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        for (int i = 59; i > 0; i--) {
            dispatch_async(dispatch_get_main_queue(), ^{
                
                _timeLab.text = [NSString stringWithFormat:@"(%zis)",i];
                
            });
            sleep(1);
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            _timeLab.hidden = YES;
            _captchaBtn.enabled = YES;
            _sendLab.hidden = YES;
            _phoneView.hidden = NO;
            [_captchaBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        });
    });
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (IBAction)backSelected:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
